﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using Common.Constants;
using Common.Helper;
using Entities;

namespace DataAccess
{
    public class ReferenceDataDataAccess : IReferenceData
    {
        /// <summary>
        /// Gets the instance.
        /// </summary>
        /// <value>
        /// The instance.
        /// </value>
        public static ReferenceDataDataAccess Instance { get; private set; }
        /// <summary>
        /// Initializes the <see cref="ContactDataAccess" /> class.
        /// </summary>
        static ReferenceDataDataAccess() { Instance = new ReferenceDataDataAccess(); }



        /// <summary>
        /// Extracts the raw data.
        /// </summary>
        /// <param name="dataReader">The data reader.</param>
        /// <returns></returns>
        private static ReferenceData ExtractRawData(IDataRecord dataReader)
        {
            var refData = new ReferenceData();
            refData.RefId = (Guid)dataReader["RefId"];
            int refCode;
            if (int.TryParse(dataReader["RefCode"].ToString(), out refCode))
                refData.RefCode = refCode;
            int refValue;
            if (int.TryParse(dataReader["RefValue"].ToString(), out refValue))
                refData.RefValue = refValue;
            refData.DataRef = dataReader["Type"].ToString();
            return refData;


        }

        public Collection<ReferenceData> GetReferenceData(int refCode)
        {
            var lstRefData = new Collection<ReferenceData>();
            

            var colParameter = new Collection<SqlParameter>
                                   {
                                       new SqlParameter("refCode", refCode)
                                      
                                       
                                   };
            using (var reader = SqlHelper.ExecuteReader(ProcedureStores.GetReferenceData, colParameter))
            {
                while (reader.Read())
                {
                    var result = ExtractRawData(reader);
                    lstRefData.Add(result);
                }
                return lstRefData;
            }
        }
    }
}
